package com.ms.p2p.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.entity.FuyouBank;
import com.ms.p2p.entity.account.AccountBank;
import com.ms.p2p.entity.account.BankCardImg;
import com.ms.p2p.entity.account.BankCardImgLog;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.provider.BankCardImgProvider;

import com.ms.p2p.provider.FuyouProvider;
import com.ms.p2p.provider.MemberAccountBankProvider;
import com.ms.p2p.provider.UnBindProvider;
import com.ms.p2p.utils.HttpTookit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping({ "/member/bank" })
public class BankController extends BaseController {

	@Reference
	private MemberAccountBankProvider bankService;

	@Reference
	private UnBindProvider bankCardLogProvider;


	@Reference
	private BankCardImgProvider bankCardImgProvider;

	@Reference
	private FuyouProvider fuyouProvider;


	@RequestMapping(value = {"/tounbind"},produces = { "application/json;charset=UTF-8" })
	public String toUnbindBankCard(HttpServletRequest request,HttpSession session) throws Exception{
		Member member = getCurrentUser();

		AccountBank accountBank = bankService.findByUserId(member.getId());
		// 根据用户ID和银行卡查询有没有申请记录，并查出正在申请状态
		BankCardImgLog bankCardImgLog = bankCardLogProvider.findByUserIdAndBankId(member.getId(),accountBank.getId(),0);
		if(bankCardImgLog!=null) {
			List<BankCardImg> list = bankCardImgProvider.findByLogId(bankCardImgLog.getId());
			request.setAttribute("images",list);
		}
		request.setAttribute("imglog",bankCardImgLog);
		return "customer/member/account/bank/unbind";
	}
	@RequestMapping(value = {"/unbind"},produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public Message unbind(HttpServletRequest request, HttpSession session) throws Exception{
		Member member = getCurrentUser();
		AccountBank accountBank = bankService.findByUserId(member.getId());
		BankCardImgLog log = new BankCardImgLog();
		log.setAddip(HttpTookit.getRealIpAddr(request));
		log.setAddtime(MsDateUtils.getTime());
		log.setBank_cardno(accountBank.getBank_cardno());
		log.setRemark("解绑");
		log.setStatus(0);
		log.setBank_id(accountBank.getId());
		log.setUser_id(member.getId());

		String[] imgall = request.getParameterValues("imgall");
		Integer user_id = member.getId();
		List<BankCardImg> list = new ArrayList<BankCardImg>();
		if (imgall != null) {
			BankCardImg img = new BankCardImg();
			int size = java.lang.reflect.Array.getLength(imgall);

			for (int i = 0; i < size; i++) {
				img.setUser_id(user_id);
				img.setImg(imgall[i]);

				list.add(img);
			}

		}

		bankCardLogProvider.insert(log,list);
		return Message.success("提交成功");
	}

	/**
	 * 保存银行卡
	 */
	@RequestMapping(value = {"/save"},produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public Message bankSave(HttpServletRequest request, HttpSession session) throws Exception{
		Member user =getCurrentUser();
		AccountBank bankCardNo = bankService.findByUserId(user.getId());
		List<FuyouBank> list = fuyouProvider.getList();
				if(bankCardNo == null ){
					bankCardNo = new AccountBank();
					bankCardNo.setBank_code(request.getParameter("bank"));
					for(FuyouBank bank:list){
						if(bank.getBank_code().equals(request.getParameter("bank"))){
							bankCardNo.setBank(bank.getBank_name());
							bankCardNo.setBank_img_mobile(bank.getBank_img_mobile());
						}
					}

					bankCardNo.setProvince(request.getParameter("s_province"));
					bankCardNo.setCity(request.getParameter("s_city"));
					bankCardNo.setArea(request.getParameter("s_county"));
					bankCardNo.setBranch(request.getParameter("branch"));
					bankCardNo.setBank_cardno(request.getParameter("bank_cardno"));
					bankCardNo.setUser_id(user.getId());
					bankCardNo.setAddtime(MsDateUtils.getTime());
					bankCardNo.setAddip(AppContext.getRemoteIp());
					bankCardNo.setStatus(1);
					bankService.insert(bankCardNo);
				} else {
					for(FuyouBank bank:list){
						if(bank.getBank_code().equals(request.getParameter("bank"))){
							bankCardNo.setBank(bank.getBank_name());
							bankCardNo.setBank_img_mobile(bank.getBank_img_mobile());
						}
					}
					bankCardNo.setBank_code(request.getParameter("bank"));
					bankCardNo.setProvince(request.getParameter("s_province"));
					bankCardNo.setCity(request.getParameter("s_city"));
					bankCardNo.setArea(request.getParameter("s_county"));
					bankCardNo.setBranch(request.getParameter("branch"));
					bankCardNo.setBank_cardno(request.getParameter("bank_cardno"));
					bankCardNo.setStatus(1);
					bankService.updateByUserId(bankCardNo);
				}


		return Message.success("保存成功");
	}
}
